package com.citcc.web.controller.business;

import com.citcc.business.bean.CdzGun;
import com.citcc.business.bean.CdzManufacturer;
import com.citcc.business.bean.CdzStake;
import com.citcc.business.bean.CdzStation;
import com.citcc.business.service.CdzGunService;
import com.citcc.business.service.CdzStakeService;
import com.citcc.common.annotation.Log;
import com.citcc.common.base.AjaxResult;
import com.citcc.common.enums.BusinessType;
import com.citcc.common.page.TableDataInfo;
import com.citcc.framework.util.ShiroUtils;
import com.citcc.framework.web.base.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/cdzGun")
public class CdzGunController extends BaseController {

    private String prefix = "business/cdzGun";

    @Autowired
    private CdzGunService cdzGunService;
    @Autowired
    private CdzStakeService cdzStakeService;

    @RequiresPermissions("business:cdzGun:view")
    @GetMapping("/main")
    public String vehicle(){
        return prefix + "/main";
    }

    @RequiresPermissions("business:cdzGun:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(CdzGun gun){
        startPage();
        if (gun.getOrgId()==null||"".equals(gun.getOrgId())) {
            gun.setOrgId(getSysUser().getDeptId());
        }
        List<CdzGun> list=cdzGunService.selectAllList(gun);
        return getDataTable(list);
    }

    /**获取充电枪列表
     * @return
     */
    @RequiresPermissions("business:cdzGun:list")
    @GetMapping("/gunList")
    @ResponseBody
    public TableDataInfo gunList(String stakeId)
    {
        startPage();
        stakeId=stakeId.substring(1,stakeId.length()-1);
        List<CdzGun> list=cdzGunService.queryByStakeCode(stakeId);
        return getDataTable(list);
    }

    @GetMapping("/add")
    public String toAdd(ModelMap map)
    {
        map.put("stakeList",cdzStakeService.selectAllList(new CdzStake()));
        return prefix + "/add";
    }

    @RequiresPermissions("business:cdzGun:add")
    @Log(title = "充电枪管理", businessType = BusinessType.INSERT)
    @PostMapping("/addSave")
    @ResponseBody
    public AjaxResult addSave(CdzGun gun)
    {
        if (gun.getOrgId()==null||"".equals(gun.getOrgId())) {
            gun.setOrgId(getSysUser().getDeptId());
        }
        gun.setCreateBy(ShiroUtils.getLoginName());
        gun.setDelFlag("0");
        return toAjax(cdzGunService.add(gun));
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id")String id, ModelMap map)
    {
        map.put("stakeList",cdzStakeService.selectAllList(new CdzStake()));
        map.put("gun",cdzGunService.getCdzGun(id));
        return prefix + "/edit";
    }


    @RequiresPermissions("business:cdzGun:edit")
    @Log(title = "充电枪管理", businessType = BusinessType.UPDATE)
    @PostMapping("/editSave")
    @ResponseBody
    public AjaxResult editSave(CdzGun gun)
    {
        gun.setUpdateBy(ShiroUtils.getLoginName());
        gun.setUpdateTime(new Date());
        return toAjax(cdzGunService.update(gun));
    }

    @RequiresPermissions("business:cdzGun:remove")
    @Log(title = "充电枪管理", businessType = BusinessType.UPDATE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String id)
    {
        try
        {
            Integer e =cdzGunService.delete(id);
            return toAjax(e);
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

}
